rm(list=ls())
set.seed(8)
setwd('~/Dropbox (Personal)/NYU/MA_Paper/Data')
needpacks = c('foreign', 'readxl', 'dplyr','stargazer','tidyverse', 'tidyr', 'sf', 'mapview', 'maps', 'lwgeom', 'ggplot2', 'rgeos', 'rnaturalearth', 'rnaturalearthdata')
sapply(needpacks,function(x) require(x,character.only = T))
theme_set(theme_bw())

#-------------------------#
# CONFEDERATE SYMBOL DATA #
#-------------------------#

# load SPLC confederate symbol data
d <- read_xlsx("confederate_data.xlsx")
attach(d)

# split the coordinates variable
class(d$Coordinates) #character object

d <- d %>% 
  separate(Coordinates, c("lat", "lon"), ",") 

# put on map
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)

locations <- st_as_sf(d, coords = c("lon", "lat"), crs = 4326)
states <- st_as_sf(map("state", plot = FALSE, fill = TRUE))
head(states)
states <- cbind(states, st_coordinates(st_centroid(states)))

counties <- st_as_sf(map("county", plot = FALSE, fill = TRUE))
counties$area <- as.numeric(st_area(counties))
head(counties)

# with state lines
ggplot(data = world) +
  geom_sf(fill="white") +
  geom_sf(data = states, fill = "white") + 
  geom_sf(data = locations, size = 1.5, shape = 23, fill = "blue") +
  coord_sf(xlim = c(-124.7844079, -66.9513812), ylim = c(24.7433195, 49.3457868), expand = FALSE)

# now differentiate the removed from the nonremoved: subset data
# active

d_active <- d[which(d$`symbol_status`=='Active'),]
locations_active <- st_as_sf(d_active, coords = c("lon", "lat"), crs = 4326)

# removed
d_removed <- d[which(d$`symbol_status`=='Removed'),]
locations_removed <- st_as_sf(d_removed, coords = c("lon", "lat"), crs = 4326)

## THIS IS FIGURE 1 IN THE PAPER
# with state lines
ggplot(data = world) +
  geom_sf(fill="white") +
  geom_sf(data = states, fill = "white") + 
  geom_sf(data = locations_active, size = 1.5, shape = 23, fill = "blue") +
  geom_sf(data = locations_removed, size = 1.5, shape = 23, fill = "red") +
  coord_sf(xlim = c(-124.7844079, -66.9513812), ylim = c(24.7433195, 49.3457868), expand = FALSE)

